# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
class ModelsEnum:
    # 模型类型
    TYPE_CHAT = "chat"
    TYPE_VECTOR = "vector"
    TYPE_RANKING = "ranking"

    @classmethod
    def get_type_desc(cls, name: str):
        _desc = {
            cls.TYPE_CHAT: "对话模型",
            cls.TYPE_VECTOR: "向量模型",
            cls.TYPE_RANKING: "重排模型"
        }
        return _desc.get(name, "")

    @classmethod
    def get_model_conf(cls, scene: str, channel: str):
        if scene == cls.TYPE_CHAT:
            return cls.chat_models().get(channel, {})
        elif scene == cls.TYPE_VECTOR:
            return cls.vector_models().get(channel, {})
        elif scene == cls.TYPE_RANKING:
            return cls.ranking_models().get(channel, {})
        else:
            return {}

    @classmethod
    def chat_models(cls):
        return {
            # 文档: https://platform.openai.com/docs/models
            "openai": {
                "name": "OpenAI",
                "logo": "static/images/models/llm_openai.png",
                "configs": [
                    {
                        "type": "group",
                        "name": "参数配置",
                        "require": True,
                        "config": [
                            {
                                "key": "context_num",
                                "name": "上下文数",
                                "type": "slider",
                                "default": 3,
                                "range": [0, 10],
                                "step": 1,
                                "tips": "根据上下文的信息进行语义理解、识别指代对象并生成连贯的回答"
                            },
                            {
                                "key": "temperature",
                                "name": "温度属性",
                                "type": "slider",
                                "default": 1.0,
                                "range": [0, 2.0],
                                "step": 0.1,
                                "tips": "用于控制生成文本的随机性，取值范围为0~2之间的浮点数，建议取值1.0左右"
                            },
                            {
                                "key": "presence_penalty",
                                "name": "存在惩罚",
                                "type": "slider",
                                "default": 0.0,
                                "range": [-2.0, 2.0],
                                "step": 0.1,
                                "tips": "用于通过惩罚已经使用的词来增加模型谈论新主题的可能性，默认0"
                            },
                            {
                                "key": "frequency_penalty",
                                "name": "频率惩罚",
                                "type": "slider",
                                "default": 0.0,
                                "range": [-2.0, 2.0],
                                "step": 0.1,
                                "tips": "用于惩罚已经频繁使用的词来降低模型一行中重复用词的可能性，默认0"
                            }
                        ]
                    },
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://api.openai.com/v1",
                    },
                    {
                        "key": "global_directives",
                        "name": "对话指令",
                        "type": "textarea",
                        "default": "",
                        "tips": "设置全局指令, 屏蔽审核人员询问模型类型等相关问题"
                    }
                ]
            },
            # 文档: https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html
            "xunfei": {
                "name": "讯飞星火",
                "logo": "static/images/models/llm_xunfei.png",
                "configs": [
                    {
                        "type": "group",
                        "name": "参数配置",
                        "require": True,
                        "config": [
                            {
                                "key": "context_num",
                                "name": "上下文数",
                                "type": "slider",
                                "default": 3,
                                "range": [0, 10],
                                "step": 1,
                                "tips": "根据上下文的信息进行语义理解、识别指代对象并生成连贯的回答"
                            },
                            {
                                "key": "temperature",
                                "name": "温度属性",
                                "type": "slider",
                                "default": 1.0,
                                "range": [0.0, 2.0],
                                "step": 0.1,
                                "tips": "用于控制生成文本的随机性，取值范围为0~2之间的浮点数，默认1.0"
                            },
                            {
                                "key": "top_k",
                                "name": "非等概率",
                                "type": "slider",
                                "default": 4,
                                "range": [1, 6],
                                "step": 1,
                                "tips": "从k个候选中随机选择⼀个，默认4"
                            },
                            {
                                "key": "presence_penalty",
                                "name": "重复惩罚",
                                "type": "slider",
                                "default": 0,
                                "range": [-2.0, 2.0],
                                "step": 0.1,
                                "tips": "用于通过惩罚已经使用的词来增加模型谈论新主题的可能性，默认0"
                            },
                            {
                                "key": "frequency_penalty",
                                "name": "频率惩罚",
                                "type": "slider",
                                "default": 0,
                                "range": [-2.0, 2.0],
                                "step": 0.1,
                                "tips": "用于惩罚已经频繁使用的词来降低模型一行中重复用词的可能性，默认0"
                            }
                        ]
                    },
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://spark-api-open.xf-yun.com/api/v1",
                    },
                    {
                        "key": "global_directives",
                        "name": "对话指令",
                        "type": "textarea",
                        "default": "",
                        "tips": "设置全局指令, 屏蔽审核人员询问模型类型等相关问题"
                    }
                ]
            },
            # 文档: https://open.bigmodel.cn/dev/api#chatglm_turbo
            "zhipu": {
                "name": "智谱清言",
                "logo": "static/images/models/llm_zhipu.png",
                "configs": [
                    {
                        "type": "group",
                        "name": "参数配置",
                        "require": True,
                        "config": [
                            {
                                "key": "context_num",
                                "name": "上下文数",
                                "type": "slider",
                                "default": 3,
                                "range": [0, 10],
                                "step": 1,
                                "tips": "根据上下文的信息进行语义理解、识别指代对象并生成连贯的回答。"
                            },
                            {
                                "key": "temperature",
                                "name": "温度属性",
                                "type": "slider",
                                "default": 0.95,
                                "range": [0, 1.0],
                                "step": 0.1,
                                "tips": "采样温度，控制输出的随机性，必须为正数取值范围是：[0.0, 1.0]，默认值为0.95。"
                            }
                        ]
                    },
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://open.bigmodel.cn/api/paas/v4",
                    },
                    {
                        "key": "global_directives",
                        "name": "系统指令",
                        "type": "textarea",
                        "default": "",
                        "tips": "设置全局指令，屏蔽审核人员询问模型类型等相关问题。"
                    }
                ]
            },
            # 文档: https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Fm2vrveyu
            "baidu": {
                "name": "百度千帆",
                "logo": "static/default/models/llm_baidu.png",
                "configs": [
                    {
                        "type": "group",
                        "name": "参数配置",
                        "require": True,
                        "config": [
                            {
                                "key": "context_num",
                                "name": "上下文数",
                                "type": "slider",
                                "default": 3,
                                "range": [0, 10],
                                "step": 1,
                                "tips": "根据上下文的信息进行语义理解、识别指代对象并生成连贯的回答。"
                            },
                            {
                                "key": "temperature",
                                "name": "温度属性",
                                "type": "slider",
                                "default": 0.95,
                                "range": [0.1, 1.0],
                                "step": 0.1,
                                "tips": "较高的数值会使输出更加随机，而较低的数值会使其更加集中和确定，默认0.95。"
                            },
                            {
                                "key": "top_p",
                                "name": "多样性值",
                                "type": "slider",
                                "default": 0.7,
                                "range": [0.1, 1.0],
                                "step": 0.1,
                                "tips": "影响输出文本的多样性，取值越大，生成文本的多样性越强，默认0.7。"
                            },
                            {
                                "key": "penalty_score",
                                "name": "重复惩罚",
                                "type": "slider",
                                "default": 1.0,
                                "range": [1.0, 2.0],
                                "step": 0.1,
                                "tips": "通过对已生成的token增加惩罚，减少重复生成的现象，"
                                        "值越大表示惩罚越大，默认1.0。"
                            }
                        ]
                    },
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://qianfan.baidubce.com/api/v2",
                    },
                    {
                        "key": "global_directives",
                        "name": "系统指令",
                        "type": "textarea",
                        "default": "",
                        "tips": "设置全局指令, 屏蔽审核人员询问模型类型等相关问题"
                    }
                ]
            },
            # 文档: https://help.aliyun.com/zh/model-studio
            "qwen": {
                "name": "通义千问",
                "logo": "static/images/models/llm_qwen.png",
                "configs": [
                    {
                        "type": "group",
                        "name": "参数配置",
                        "require": True,
                        "config": [
                            {
                                "key": "context_num",
                                "name": "上下文数",
                                "type": "slider",
                                "default": 3,
                                "range": [0, 10],
                                "step": 1,
                                "tips": "根据上下文的信息进行语义理解、识别指代对象并生成连贯的回答"
                            },
                            {
                                "key": "temperature",
                                "name": "温度属性",
                                "type": "slider",
                                "default": 0.95,
                                "range": [0, 2.0],
                                "step": 0.1,
                                "tips": "较高的数值会使输出更加随机，而较低的数值会使其更加集中和确定，默认0.95。"
                            },
                            {
                                "key": "top_p",
                                "name": "多样性值",
                                "type": "slider",
                                "default": 0.85,
                                "range": [0.0, 1.0],
                                "step": 0.1,
                                "tips": "影响输出文本的多样性，取值越大，生成文本的多样性越强，默认0.85"
                            },
                            {
                                "key": "top_k",
                                "name": "采样候选",
                                "type": "slider",
                                "default": 20,
                                "range": [1, 150],
                                "step": 1,
                                "tips": "取值越大，生成的随机性越高；取值越小，生成的确定性越高, 值大于100时，则不启用top_k策略"
                            },
                            {
                                "key": "presence_penalty",
                                "name": "重复惩罚",
                                "type": "slider",
                                "default": 0,
                                "range": [-2.0, 2.0],
                                "step": 0.1,
                                "tips": "用于通过惩罚已经使用的词来增加模型谈论新主题的可能性，默认0"
                            },
                            {
                                "key": "repetition_penalty",
                                "name": "连续惩罚",
                                "type": "slider",
                                "default": 1.0,
                                "range": [0.1, 2.0],
                                "step": 0.1,
                                "tips": "模型生成时连续序列中的重复度。可以降低模型生成的重复度，1.0表示不做惩罚。"
                            },
                            {
                                "key": "enable_search",
                                "name": "联网搜索",
                                "type": "switch",
                                "default": False,
                                "tips": "启用互联网搜索，模型会将搜索结果作为文本生成过程中的参考信息，默认false，表示关闭"
                            }
                        ]
                    },
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://api.baichuan-ai.com/api/v1",
                    },
                    {
                        "key": "global_directives",
                        "name": "对话指令",
                        "type": "textarea",
                        "default": "",
                        "tips": "设置全局指令, 屏蔽审核人员询问模型类型等相关问题"
                    }
                ]
            },
            # 文档: https://platform.baichuan-ai.com
            "baichuan": {
                "name": "百川模型",
                "logo": "static/images/models/llm_baichuan.png",
                "configs": [
                    {
                        "type": "group",
                        "name": "参数配置",
                        "require": True,
                        "config": [
                            {
                                "key": "context_num",
                                "name": "上下文数",
                                "type": "slider",
                                "default": 3,
                                "range": [0, 10],
                                "step": 1,
                                "tips": "根据上下文的信息进行语义理解、识别指代对象并生成连贯的回答"
                            },
                            {
                                "key": "temperature",
                                "name": "温度属性",
                                "type": "slider",
                                "default": 0.3,
                                "range": [0, 1.0],
                                "step": 0.1,
                                "tips": "较高的数值会使输出更加随机，而较低的数值会使其更加集中和确定，默认0.3。"
                            },
                            {
                                "key": "top_p",
                                "name": "多样性值",
                                "type": "slider",
                                "default": 0.85,
                                "range": [0.0, 1.0],
                                "step": 0.1,
                                "tips": "影响输出文本的多样性，取值越大，生成文本的多样性越强，默认0.85"
                            },
                            {
                                "key": "top_k",
                                "name": "采样控制",
                                "type": "slider",
                                "default": 5,
                                "range": [1, 20],
                                "step": 1,
                                "tips": "越大: 采样集大, 0则不走采样策略, 0 则不走 top_k 采样筛选策略, 默认5"
                            }
                        ]
                    },
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://api.baichuan-ai.com/api/v1",
                    },
                    {
                        "key": "global_directives",
                        "name": "对话指令",
                        "type": "textarea",
                        "default": "",
                        "tips": "设置全局指令, 屏蔽审核人员询问模型类型等相关问题"
                    }
                ]
            }
        }

    @classmethod
    def vector_models(cls):
        return {
            "openai": {
                "logo": "static/images/models/llm_openai.png",
                "name": "OpenAI",
                "configs": [
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://api.openai.com/v1",
                    }
                ]
            },
            "xunfei": {
                "logo": "static/images/models/llm_xunfei.png",
                "name": "讯飞星火",
                "configs": [
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://emb-cn-huabei-1.xf-yun.com",
                    }
                ]
            },
            "zhipu": {
                "logo": "static/images/models/llm_zhipu.png",
                "name": "智谱清言",
                "configs": [
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 默认: https://open.bigmodel.cn",
                    }
                ]
            },
            "qwen": {
                "logo": "static/images/models/llm_qwen.png",
                "name": "通义千问",
                "configs": [
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 不填写则默认为: https://dashscope.aliyuncs.com"
                    }
                ]
            }
        }

    @classmethod
    def ranking_models(cls):
        return {
            "general": {
                "logo": "static/images/models/llm_openai.png",
                "name": "通用通道",
                "configs": [
                    {
                        "key": "check_key",
                        "name": "校验密钥",
                        "type": "switch",
                        "default": True,
                        "tips": "用于应对本地部署模型不需要填密钥的场景",
                    },
                    {
                        "key": "agency_api",
                        "name": "代理域名",
                        "type": "input",
                        "default": "",
                        "tips": "反向代理API域名, 比如: https://api.xx.com/api/v1",
                    }
                ]
            }
        }
